const router = require('koa-router')()

class Router {
    constructor(path, method, isNoAuth) {
        //路由路径
        this.path = path;
        //请求方式
        this.method = method;
        //中间件
        this.intermediateKey = [];
        //是否放行
        this.isNoAuth = isNoAuth || false;
        return this;
    }

    //处理
    handle(ctx, next) {
        ctx.state = 404;
        ctx.body = "访问失败,路径未定义";
    }

    //添加中间件
    addIntermediateKey(func) {
        this.intermediateKey.push(func);
    }




    build(router) {
        //加上最终的处理函数
        this.intermediateKey.push(this.handle);
        //给中间件函数绑定this
        const boundMiddleware = this.intermediateKey.map(fn => fn.bind(this));
        //绑定路径
        router = router[this.method](this.path, ...boundMiddleware);
        //放行端口
        this.isNoAuth && router.noAuth();
        return router;
    }
}

module.exports = Router;
